package com.shixianchen.review_the_day_before.day4;

import com.shixianchen.utils.Utils;
import com.shixianjun.sort.Sortable;
import com.shixianjun.sort.SortableElement;
//import jdk.nashorn.internal.ir.CallNode;

/**
 * @author ShiXianChen
 * @version V1.0.0
 * @Description 选择排序 优化
 * @date 2022/11/3 21:24
 */
public class SelectionSort implements Sortable {

    @Override
    public void sort(SortableElement[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            int minIndex = i;
            int maxIndex = n -1;


            //获取最小的下标
            for (int j = i; j < n; j++) {
                minIndex = arr[minIndex].value > arr[j].value ? j : minIndex;
                maxIndex = arr[maxIndex].value < arr[j].value ? j : maxIndex;
            }

            Utils.swap(arr,minIndex,i);

            if (maxIndex == i){
                maxIndex = minIndex;
            }

            Utils.swap(arr,maxIndex,n-1);
            n = n-1;
        }
    }
}
